package com.bawei.module_mine.ui

import android.util.Log
import android.view.View
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.bawei.lib_common.mvi.base.BaseMVIActivity
import com.bawei.module_mine.R
import com.bawei.module_mine.databinding.ActivityRegBinding
import com.bawei.module_mine.intent.RegisterIntent
import com.bawei.module_mine.state.RegisterState
import com.bawei.module_mine.viewmodel.RegisterViewModel
import kotlinx.coroutines.launch

@Route(path = "/module_mine/RegActivity")
class RegActivity : BaseMVIActivity() {
    lateinit var binding: ActivityRegBinding
    var viewModel: RegisterViewModel? = null
    override fun generatStatusBarColor(): Int {
        return R.color.colorPrimary
    }

    override fun initData() {
        super.initData()
    }
    override fun generateViewModel() {
       viewModel = RegisterViewModel()
    }

    override fun getLayoutRootView(): View {
        binding = ActivityRegBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun handleState() {
        lifecycleScope.launch {
            viewModel?.state?.collect{ state ->
                when(state){
                    is RegisterState.registerSuccess -> {
                        try {
                            if (state.result!=null){
                                Log.d("RegActivity", "注册成功")
                                showMsg("注册成功")
                                closeThis()
                            } else {
                                Log.e("RegActivity", "注册成功但用户信息为空")
                                showMsg("注册成功但用户信息异常")
                                // 恢复按钮状态
                                binding.btnMineRegReg.isEnabled = true
                                binding.btnMineRegReg.text = "注册"
                            }
                        } catch (e: Exception) {
                            Log.e("RegActivity", "处理注册成功状态异常: ${e.message}", e)
                            showMsg("注册成功但处理信息失败")
                            // 恢复按钮状态
                            binding.btnMineRegReg.isEnabled = true
                            binding.btnMineRegReg.text = "注册"
                        }
                    }
                    is RegisterState.registerFailed -> {
                        try {
                            Log.d("RegActivity", "注册失败")
                            showMsg("注册失败，请检查用户名是否已存在")
                            // 恢复按钮状态
                            binding.btnMineRegReg.isEnabled = true
                            binding.btnMineRegReg.text = "注册"
                        } catch (e: Exception) {
                            Log.e("RegActivity", "处理注册失败状态异常: ${e.message}", e)
                            showMsg("注册失败，请重试")
                            // 恢复按钮状态
                            binding.btnMineRegReg.isEnabled = true
                            binding.btnMineRegReg.text = "注册"
                        }
                    }
                    is RegisterState.registerInit -> {
                        Log.d("RegActivity", "注册状态初始化")
                        // 确保按钮状态正常
                        binding.btnMineRegReg.isEnabled = true
                        binding.btnMineRegReg.text = "注册"
                    }
                }
            }
        }
    }

    override fun initEvent() {
        super.initEvent()
        binding.btnMineRegReg.setOnClickListener {
            try {
                val username = binding.etMineRegUsername.text.trim().toString()
                val password = binding.etMineRegPwd.text.trim().toString()
                val repassword = binding.etMineRegPwdAgain.text.trim().toString()

                if (username.isEmpty()){
                    showMsg("请输入用户名")
                    return@setOnClickListener
                }
                if (password.isEmpty()){
                    showMsg("请输入密码")
                    return@setOnClickListener
                }
                if (repassword.isEmpty()){
                    showMsg("请输入确认密码")
                    return@setOnClickListener
                }
                if (password!=repassword){
                    showMsg("密码不一致")
                    return@setOnClickListener
                }
                
                // 显示加载状态
                binding.btnMineRegReg.isEnabled = false
                binding.btnMineRegReg.text = "注册中..."
                
                lifecycleScope.launch {
                    try {
                        Log.d("RegActivity", "发送注册请求")
                        viewModel?.intents?.send(RegisterIntent.register(username, password))
                    } catch (e: Exception) {
                        Log.e("RegActivity", "发送注册请求异常: ${e.message}", e)
                        showMsg("注册请求失败，请重试")
                        // 恢复按钮状态
                        binding.btnMineRegReg.isEnabled = true
                        binding.btnMineRegReg.text = "注册"
                    }
                }
            } catch (e: Exception) {
                Log.e("RegActivity", "注册按钮点击异常: ${e.message}", e)
                showMsg("操作异常，请重试")
                // 恢复按钮状态
                binding.btnMineRegReg.isEnabled = true
                binding.btnMineRegReg.text = "注册"
            }
        }
    }

}